package com.templeix.jfinal.interceptor;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.log.Logger;
import com.templeix.constant.Attr;
import com.templeix.jfinal.base.BaseService;
import com.templeix.timeaxis.model.SysMenu;
import com.templeix.timeaxis.model.SysRole;


/**
 * 用户认证拦截器
 * 
 * 
 */
public class UserAuthInterceptor implements Interceptor {

	private static final Logger log = Logger.getLogger(UserAuthInterceptor.class);

	public void intercept(Invocation ai) {

		Controller controller = ai.getController();

		HttpServletRequest request = controller.getRequest();
		String referer = request.getHeader("referer");
		String site = "http://" + request.getServerName();
		
		log.info("####IP:" + request.getRemoteAddr() + 
					"\t port:" + request.getRemotePort() + 
					"\t 请求路径:"+ request.getRequestURI());
		
		if (referer == null || !referer.startsWith(site)) {
			log.warn("####非法的请求");
		}

		
		Map<Integer, List<SysMenu>> map  = controller.getSessionAttr(Attr.SESSION_MENU);
		if(map == null) {
			//注入普通用户权限菜单
			//map = BaseService.getMenuMap(SysRole.ROLE_COMMON);
			map = BaseService.getMenuMap(SysRole.ROLE_ADMIN);
			// 注入菜单
			controller.setSessionAttr("menu", map);
		}
		ai.invoke();
	}
}
